package com.gc.web.controller.api;

import com.gc.business.domain.ElevatorBrand;
import com.gc.business.domain.ElevatorModel;
import com.gc.business.domain.FaultManage;
import com.gc.business.service.IElevatorBrandService;
import com.gc.business.service.IElevatorModelService;
import com.gc.business.service.IFaultManageService;
import com.gc.common.constant.Constants;
import com.gc.common.utils.StringUtils;
import com.gc.framework.web.base.ApiBaseController;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 故障管理 控制层
 * 
 * @author 刘锦腾
 * @date 2020-12-09
 */
@Slf4j
@Controller
@RequestMapping("/api/faultManage")
public class ApiFaultManageController extends ApiBaseController {

	@Autowired
	private IElevatorBrandService elevatorBrandService;

	@Autowired
	private IElevatorModelService elevatorModelService;

	@Autowired
	private IFaultManageService faultManageService;
	
	/**
	 * 选择电梯
	 */
	@GetMapping( "/selectElevatorBrandList")
	@ResponseBody
	public Map<String, Object> selectElevatorBrandList(Integer pageNum,Integer pageSize){
		startPage(pageNum,pageSize,"create_time DESC");
		List<ElevatorBrand> elevatorBrandList = elevatorBrandService.list();
		return success(elevatorBrandList);
	}

	/**
	 * 选择型号
	 */
	@GetMapping( "/selectElevatorModelList")
	@ResponseBody
	public Map<String, Object> selectElevatorModelList(Long brandId, Integer pageNum,Integer pageSize){
		if (brandId == null){
			return error(Constants.UNVERIFY,"请选择电梯品牌");
		}
		startPage(pageNum,pageSize,"create_time DESC");
		List<ElevatorModel> elevatorModelList = elevatorModelService.list(new ElevatorModel() {{
			setBrandId(brandId);
		}});
		return success(elevatorModelList);
	}

	/**
	 * 故障码查询
	 *
	 * @return
	 */
	@GetMapping("/selectFaultCode")
	@ResponseBody
	public Map<String, Object> selectFaultCode(Long brandId, Long modelId, String faultCode, Integer pageNum, Integer pageSize) {
		if (brandId == null) {
			return error(Constants.UNVERIFY, "请选择电梯品牌");
		}
		if (modelId == null) {
			return error(Constants.UNVERIFY, "请选择电梯型号");
		}
		if (StringUtils.isEmpty(faultCode)) {
			return error(Constants.UNVERIFY, "请输入故障单号");
		}
		startPage(pageNum, pageSize, "create_time DESC");
		List<FaultManage> faultManageList = faultManageService.selectFaultCodeList(brandId,modelId,faultCode);
		Map<String, Object> mapReturn = new HashMap<>();
		mapReturn.put("pageNum", pageNum);
		mapReturn.put("list", faultManageList);
		mapReturn.put("total", new PageInfo<>(faultManageList).getTotal());
		return success(mapReturn);
	}

}
